<?php

class Application_Model_Mapper_NachrichtenMapper
{
	protected $_dbTable;

	public function setDbTable ($dbTable)
	{
		
		if (is_string($dbTable))
		{
			$dbTable = new $dbTable();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract)
		{
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
		
	}

	public function getDbTable ()
	{
		
		if (null === $this->_dbTable)
		{
			$this->setDbTable('Application_Model_DbTable_Nachrichten');
		}
		return $this->_dbTable;
	}

	public function add (Application_Model_Nachrichten $nachricht, $id)
	{	
		

		$now = new DateTime();
		$generatedid = Helper_Idgenerator::getid();
		$generatedidcheck = $this->fetchID($generatedid);
		if(empty($generatedidcheck)){
		
			$data = array(
				'id' => $generatedid,
				'created' => date('Y-m-d H:i:s'),
				'empfanger' => $nachricht->getEmpfanger(),
				'titel' => $nachricht->getTitel(),
				'absender' => $id,
				'read' => '0',
				'delete' => '0',
				'system' => '0',
				'text' => $nachricht->getText());

			return $this->getDbTable()->insert($data);
		}
		else{
			$this->add($nachricht, $id);
		}
		
	}

	public function addfl (Application_Model_Nachrichten $nachricht, $id)
	{		
		$now = new DateTime();
		$generatedid = Helper_Idgenerator::getid();
		$generatedidcheck = $this->fetchID($generatedid);
		if(empty($generatedidcheck)){
		
			$data = array(
				'id' => $generatedid,
				'created' => date('Y-m-d H:i:s'),
				'empfanger' => $nachricht->getEmpfanger(),
				'titel' => $nachricht->getTitel(),
				'absender' => $id,
				'read' => '0',
				'delete' => '0',
				'system' => '1',
				'text' => $nachricht->getText());

			return $this->getDbTable()->insert($data);
		}
		else{
			$this->addfl($nachricht, $id);
		}
		
	}
	

	public function delete($id)
	{
		$mappernachricht = new Application_Model_Mapper_NachrichtenMapper();
		$eintrag = $mappernachricht->fetchID($id);
		
		
		$mapperauth = new Application_Model_Mapper_AuthMapper();
		$user = $mapperauth->fetchID($eintrag['empfanger']);
		

		if($user['papierkorbnachrichten']){
			if(!$eintrag['delete']){
				$data = array(
						'delete' => true);
				return $this->getDbTable()->update($data, array(
						'id = ?' => $id));
			}
			else{
				$table = $this->getDbTable();
				$where = $table->getAdapter()->quoteInto('id = ?', $id);
				$table->delete($where);
			}
		}
		else{
			$table = $this->getDbTable();
			$where = $table->getAdapter()->quoteInto('id = ?', $id);
			$table->delete($where);
		}
		
// 		$this->getDbTable()->delete('id =' . $id);
	}
	
	public function wiederherstellen($id)
	{
		$data = array(
				'delete' => (false));
		return $this->getDbTable()->update($data, array(
				'id = ?' => $id));
	}
	
	public function nachrichtgelesen($id)
	{
	
		$data = array(
				'read' => true);
		return $this->getDbTable()->update($data, array(
				'id = ?' => $id));
	
	}

	public function fetchID ($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('nachrichten', array('id AS messageid', 'text', 'titel', 'created', 'read', 'absender', 'system', 'empfanger'));
		$entry->joinleft(array('user' => 'users', array('id' => 'id')),
				'nachrichten.absender = user.id');
		$entry->where('nachrichten.id = ?', $id);
		return $entry->query()->fetch();
	}

	public function fetchAllUsermsg($id)
	{
 		$db = Zend_Db_Table::getDefaultAdapter();
 		$entry = new Zend_Db_Select($db);
		$entry->from('nachrichten', array('id AS messageid', 'text', 'titel', 'created AS created_first', 'read'));
		$entry->joinleft(array('user' => 'users', array('id' => 'id')),
				'nachrichten.absender = user.id');
		$entry->where('nachrichten.empfanger = ?', $id);
		$entry->where('nachrichten.delete = 0');
		$entry->order('created_first DESC');
		return $entry->query()->fetchall();
	}

	public function fetchAlldeletedUsermsg($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
// 		$entry->from('nachrichten');
		$entry->from('nachrichten', array('id AS messageid', 'text', 'titel' ));
		$entry->joinleft(array('user' => 'users', array('id' => 'id')),
				'nachrichten.absender = user.id');
		$entry->where('nachrichten.empfanger = ?', $id);
		$entry->where('nachrichten.delete = 1');
		return $entry->query()->fetchall();
	}
	
	public function fetchAllunreadUsermsg($id)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('nachrichten');
		$entry->where('nachrichten.empfanger = ?', $id);
		$entry->where('nachrichten.delete = 0');
		$entry->where('nachrichten.read = 0');
		return count($entry->query()->fetchall());
	}


}